﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;//Please add references
namespace MxWeiXinPF.DAL
{
	/// <summary>
	/// 数据访问类:shuifei_baseinfo
	/// </summary>
	public partial class shuifei_baseinfo
	{
		public shuifei_baseinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "shuifei_baseinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from shuifei_baseinfo");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(MxWeiXinPF.Model.shuifei_baseinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into shuifei_baseinfo(");
			strSql.Append("wid,contact_person,tel,api_url,api_UserName,api_Key,index_bg_pic,tips,remark,create_time,update_time,create_person)");
			strSql.Append(" values (");
			strSql.Append("@wid,@contact_person,@tel,@api_url,@api_UserName,@api_Key,@index_bg_pic,@tips,@remark,@create_time,@update_time,@create_person)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@wid", SqlDbType.Int,4),
					new SqlParameter("@contact_person", SqlDbType.VarChar,250),
					new SqlParameter("@tel", SqlDbType.VarChar,1000),
					new SqlParameter("@api_url", SqlDbType.VarChar,300),
					new SqlParameter("@api_UserName", SqlDbType.VarChar,50),
					new SqlParameter("@api_Key", SqlDbType.VarChar,50),
					new SqlParameter("@index_bg_pic", SqlDbType.VarChar,350),
					new SqlParameter("@tips", SqlDbType.VarChar,1000),
					new SqlParameter("@remark", SqlDbType.VarChar,1500),
					new SqlParameter("@create_time", SqlDbType.DateTime),
					new SqlParameter("@update_time", SqlDbType.DateTime),
					new SqlParameter("@create_person", SqlDbType.VarChar,50)};
			parameters[0].Value = model.wid;
			parameters[1].Value = model.contact_person;
			parameters[2].Value = model.tel;
			parameters[3].Value = model.api_url;
			parameters[4].Value = model.api_UserName;
			parameters[5].Value = model.api_Key;
			parameters[6].Value = model.index_bg_pic;
			parameters[7].Value = model.tips;
			parameters[8].Value = model.remark;
			parameters[9].Value = model.create_time;
			parameters[10].Value = model.update_time;
			parameters[11].Value = model.create_person;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(MxWeiXinPF.Model.shuifei_baseinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update shuifei_baseinfo set ");
			strSql.Append("wid=@wid,");
			strSql.Append("contact_person=@contact_person,");
			strSql.Append("tel=@tel,");
			strSql.Append("api_url=@api_url,");
			strSql.Append("api_UserName=@api_UserName,");
			strSql.Append("api_Key=@api_Key,");
			strSql.Append("index_bg_pic=@index_bg_pic,");
			strSql.Append("tips=@tips,");
			strSql.Append("remark=@remark,");
			strSql.Append("create_time=@create_time,");
			strSql.Append("update_time=@update_time,");
			strSql.Append("create_person=@create_person");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@wid", SqlDbType.Int,4),
					new SqlParameter("@contact_person", SqlDbType.VarChar,250),
					new SqlParameter("@tel", SqlDbType.VarChar,1000),
					new SqlParameter("@api_url", SqlDbType.VarChar,300),
					new SqlParameter("@api_UserName", SqlDbType.VarChar,50),
					new SqlParameter("@api_Key", SqlDbType.VarChar,50),
					new SqlParameter("@index_bg_pic", SqlDbType.VarChar,350),
					new SqlParameter("@tips", SqlDbType.VarChar,1000),
					new SqlParameter("@remark", SqlDbType.VarChar,1500),
					new SqlParameter("@create_time", SqlDbType.DateTime),
					new SqlParameter("@update_time", SqlDbType.DateTime),
					new SqlParameter("@create_person", SqlDbType.VarChar,50),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.wid;
			parameters[1].Value = model.contact_person;
			parameters[2].Value = model.tel;
			parameters[3].Value = model.api_url;
			parameters[4].Value = model.api_UserName;
			parameters[5].Value = model.api_Key;
			parameters[6].Value = model.index_bg_pic;
			parameters[7].Value = model.tips;
			parameters[8].Value = model.remark;
			parameters[9].Value = model.create_time;
			parameters[10].Value = model.update_time;
			parameters[11].Value = model.create_person;
			parameters[12].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from shuifei_baseinfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from shuifei_baseinfo ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.shuifei_baseinfo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,wid,contact_person,tel,api_url,api_UserName,api_Key,index_bg_pic,tips,remark,create_time,update_time,create_person from shuifei_baseinfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			MxWeiXinPF.Model.shuifei_baseinfo model=new MxWeiXinPF.Model.shuifei_baseinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.shuifei_baseinfo DataRowToModel(DataRow row)
		{
			MxWeiXinPF.Model.shuifei_baseinfo model=new MxWeiXinPF.Model.shuifei_baseinfo();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["wid"]!=null && row["wid"].ToString()!="")
				{
					model.wid=int.Parse(row["wid"].ToString());
				}
				if(row["contact_person"]!=null)
				{
					model.contact_person=row["contact_person"].ToString();
				}
				if(row["tel"]!=null)
				{
					model.tel=row["tel"].ToString();
				}
				if(row["api_url"]!=null)
				{
					model.api_url=row["api_url"].ToString();
				}
				if(row["api_UserName"]!=null)
				{
					model.api_UserName=row["api_UserName"].ToString();
				}
				if(row["api_Key"]!=null)
				{
					model.api_Key=row["api_Key"].ToString();
				}
				if(row["index_bg_pic"]!=null)
				{
					model.index_bg_pic=row["index_bg_pic"].ToString();
				}
				if(row["tips"]!=null)
				{
					model.tips=row["tips"].ToString();
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
				if(row["create_time"]!=null && row["create_time"].ToString()!="")
				{
					model.create_time=DateTime.Parse(row["create_time"].ToString());
				}
				if(row["update_time"]!=null && row["update_time"].ToString()!="")
				{
					model.update_time=DateTime.Parse(row["update_time"].ToString());
				}
				if(row["create_person"]!=null)
				{
					model.create_person=row["create_person"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,wid,contact_person,tel,api_url,api_UserName,api_Key,index_bg_pic,tips,remark,create_time,update_time,create_person ");
			strSql.Append(" FROM shuifei_baseinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,wid,contact_person,tel,api_url,api_UserName,api_Key,index_bg_pic,tips,remark,create_time,update_time,create_person ");
			strSql.Append(" FROM shuifei_baseinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM shuifei_baseinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from shuifei_baseinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "shuifei_baseinfo";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MxWeiXinPF.Model.shuifei_baseinfo GetModelByWid(int wid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,wid,contact_person,tel,api_url,api_UserName,api_Key,index_bg_pic,tips,remark,create_time,update_time,create_person from shuifei_baseinfo ");
            strSql.Append(" where wid=@wid");
            SqlParameter[] parameters = {
                    new SqlParameter("@wid", SqlDbType.Int,4)
            };
            parameters[0].Value = wid;

            MxWeiXinPF.Model.shuifei_baseinfo model = new MxWeiXinPF.Model.shuifei_baseinfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        #endregion  ExtensionMethod
    }
}

